const crypto = require('crypto');
const path = require('path');

// 讯达云 设置
// http://apidocs.speedycloud.cn/?api=s3#api-s3API-download_bucket_object

const ACCESS = '6D1655CCDC9C91826B5F0609C03E0D93';
const Secret = '3eef6425f9f5c6647516c765ddf46dbb215e1b0cc09cf7e291a4954a29f0720e';

const LINK = 'http://oss-cn-beijing.speedycloud.org';
const basePath = path.join(process.cwd(), '/tmp');


/**
 * 返回文件地址
 * @param {*} fileName
 * @param {*} realUrl
 * @returns
 */
function turnURL(fileName, realUrl) {
  return `${ realUrl ? LINK : '' }/aiimage/${fileName}`;
}


/**
 * 签名
 * @param {*} httpMethod 
 * @param {*} contentType 
 * @param {*} url 
 * @param {*} date 
 */
function createSign(httpMethod, contentType, url, date) {
  const signStr = `${httpMethod}\n\n${contentType}\n${date}\n${url}`;
  const hash = crypto.createHmac('sha1', Secret);
  hash.update(signStr);

  return `AWS ${ACCESS}:${ hash.digest().toString('base64') }`;
}

module.exports = {
  LINK,
  basePath,
  createSign,
  turnURL
};